﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace WisPortal
{
    /// <summary>
    /// UI class is a helper class which provides user interface related functions
    /// </summary>
    public class UI
    {
        public static string GetRelativeUrl(string appUrl, string url)
        {
            return url.Replace("~/",appUrl);
        }

        public static string GetThemePath()
        {
            return GetThemePath("");
        }
        /// <summary>
        /// Get the theme file path
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        public static string GetThemePath(string path)
        {
            if (path.StartsWith("/"))
                path = path.TrimStart("/".ToCharArray());
            return "~/Content/themes/base/" + path;
        }

        public static string Localize(string key)
        {
            return Localize(key, key);
        }

        /// <summary>
        /// Get the localized string by current setttings
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public static string Localize(string key,string title)
        {
            ////To be implemented
            return title;
        }

        /// <summary>
        /// Change the parameter's value in a url with the newValue
        /// </summary>
        /// <param name="url">The url must have the pattern like "key=xxx",and the value must be end with "&" or the last word.</param>
        /// <param name="key">The parameter's name</param>
        /// <param name="newValue"></param>
        /// <returns></returns>
        public static string ReplaceParameterValue(string url, string key, string newValue)
        {
            int startPos = url.ToLower().IndexOf(string.Format("{0}=",key.ToLower()));
            if (startPos > -1)
            {
                //Move to the end of "="
                startPos += key.Length + 1;

                //Find the value position
                int endPos = url.Substring(startPos).IndexOf("&");
                if (endPos > -1)
                {
                    return string.Format("{0}{1}{2}", url.Substring(0, startPos), newValue, url.Substring(endPos));
                }
                else
                    return string.Format("{0}{1}", url.Substring(0, startPos), newValue);
            }
            else
            {
                string connector = "&";
                if(url.IndexOf("?") < 0)
                    connector = "?";
                return url + string.Format("{2}{0}={1}",key,newValue,connector);
            }                
        }

        /// <summary>
        /// Is Match a pattern
        /// </summary>
        /// <param name="input"></param>
        /// <param name="patten"></param>
        /// <returns></returns>
        public static bool IsMatch(string input, string patten)
        {
            System.Text.RegularExpressions.Regex reg = new System.Text.RegularExpressions.Regex(patten);
            return reg.IsMatch(input);
        }

        public static bool IsEmail(string input)
        {
            return IsMatch(input,"\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*");
        }

        public static bool IsGuid(string input)
        {
            return IsMatch(input, "(\\{?)[A-F0-9]{8}(-[A-F0-9]{4}){3}-[A-F0-9]{12}(\\}?)");
        }

        public static bool IsUrl(string input)
        {
            return IsMatch(input, "[a-zA-z]+://[^\\s]*");
        }
    }
}
